<?php
/**
 * 
 * @author yuxun
 *
 */

class ModSevenRoleInfo {
	
	
    public function select($params){
		$dbLink = new PubDbLink('dbSelf', PubDatabase::getDriveDbName($platform,$params['g']));
		
		$start_ym = strtotime(date('Ym01 00:00:00',strtotime($params['open_time'])));
		$end_ym = strtotime(date('Ym01 00:00:00',strtotime($params['seven_open_time'])));

		$monarr = array();
		$monarr[] = date('Ym',$start_ym);
		while( ($start_ym = strtotime('+1 month', $start_ym)) <= $end_ym){
			$monarr[] = date('Ym',$start_ym); // 取得递增月;
		}
	
		$where  = ' WHERE RecDate BETWEEN :begin AND :end AND Period = :period AND GameSrc=:game ';
		$args   = array(
				':begin'    => $params['open_time'],
				':end'      => $params['seven_open_time'],
				':period'   => "Daily",
				':game'     => $params['g'],
		);
	
		if(!empty($params['RoleId'])){
			$where .= " AND RoleId LIKE :RoleId ";
			$args['RoleId'] = '%' . $params['RoleId'] . '%';
		}
	
		if(!empty($params['RoleName'])){
			$where .= " AND RoleName LIKE :RoleName ";
			$args['RoleName'] = '%' . $params['RoleName'] . '%';
		}
	
	
		$user = AccAuth::me();
		if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
			$where .= " AND Platform=:p ";
			$args[':p'] = $params['os'];
		}
		
		$where .= " AND SID=:ser ";
		$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];

		$table  = "(";
		foreach ($monarr as $key=>$val){
			$oneTb = "tblgameroleinfo_".$val;
			$table .= " (SELECT * FROM {$oneTb} {$where}) UNION ALL";
		}
		$table = substr($table, 0, -9);
		$table .= ') AS tbl';
		$order  = self::getOrder($params['order']);
		$fields = "*";
		$sql = "SELECT {$fields} FROM {$table} {$where} {$order}";
		$data = $dbLink->query($sql, $args);
		return $data;
	}
	
	
	public static function getOrder($order){
		if(PubFunc::checkEmpty($order, array(0, 1))) return '';
		if(!in_array($order[1], array('asc', 'desc'))){
			$sort = 'asc';
		}else{
			$sort = $order[1];
		}
		return " ORDER BY {$order[0]} {$sort} ";
	}
	

    
}